package game.block.screen;

import game.block.Constants;
import game.block.Locator;
import game.block.game.GameState;

import java.util.ArrayList;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureWrap;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;

public class MainMenuScreen implements Screen, Constants {
	private BitmapFont white, white96;
	private Stage stage;
	private Table table;
	private TextButton buttonExit, buttonPlay, buttonLevel, buttonClear;
	private Label heading;
	private Skin skin;
	private TextureAtlas atlas;
	private TextButtonStyle textButtonStyle, playStyle, exitStyle, clearStyle, levelStyle;
	private final ArrayList<TransitionEffect> effects;
	private Texture background;
	private SpriteBatch uiBatch;
	public static int COUNT=3;

	public MainMenuScreen(final GameState gs, final Game game) {
		stage = new Stage();
		uiBatch=new SpriteBatch();
		background = new Texture(BACKGROUNDMAIN);
        background.setWrap(TextureWrap.Repeat, TextureWrap.Repeat);
		white = new BitmapFont(Gdx.files.internal("data/white2.fnt"), false);
		white96 = new BitmapFont(Gdx.files.internal("data/white2.fnt"), false);
		atlas = new TextureAtlas(Gdx.files.internal("data/button.pack"));
		skin = new Skin(atlas);
		table = new Table(skin);
		table.setBounds(-Gdx.graphics.getWidth() / 8, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

		textButtonStyle = new TextButtonStyle();
		textButtonStyle.up = skin.getDrawable("button.up");
		textButtonStyle.down = skin.getDrawable("button.down");
		textButtonStyle.pressedOffsetX = 1;
		textButtonStyle.pressedOffsetY = -1;
		textButtonStyle.font = white;
		playStyle = new TextButtonStyle(textButtonStyle);
		playStyle.fontColor = Color.WHITE;
		exitStyle = new TextButtonStyle(textButtonStyle);
		exitStyle.fontColor = Color.WHITE;
		clearStyle = new TextButtonStyle(textButtonStyle);
		clearStyle.fontColor = Color.WHITE;
		levelStyle = new TextButtonStyle(textButtonStyle);
		levelStyle.fontColor = Color.WHITE;

		// create a transition screen
		final Screen current = this;
		effects = new ArrayList<TransitionEffect>();
		effects.add(new FadeOutTransitionEffect(0.5f));
		effects.add(new FadeInTransitionEffect(0.5f));

		final BoardScreen next = gs.getBoardScreen();
		buttonPlay = new TextButton("Continue", playStyle);
		buttonPlay.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				Locator.getAudio().playSound(SOUND.HOVER.getValue());
				Screen transitionScreen = new TransitionScreen(game, current, next, effects);
				game.setScreen(transitionScreen);
			}
		});
		buttonPlay.pad(10.0f);

		buttonLevel = new TextButton("Level Select", levelStyle);
		buttonLevel.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				Locator.getAudio().playSound(SOUND.HOVER.getValue());
				Screen transitionScreen = new TransitionScreen(game, current, gs.getLevelScreen(), effects);
				game.setScreen(transitionScreen);
			}
		});
		buttonLevel.pad(10.0f);

		buttonClear = new TextButton("New Game", clearStyle);
		buttonClear.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				Locator.clearData();
				next.newGame();
				Locator.getAudio().playSound(SOUND.HOVER.getValue());
				Screen transitionScreen = new TransitionScreen(game, current, next, effects);
				game.setScreen(transitionScreen);
			}
		});
		buttonClear.pad(10.0f);

		// create buttons
		buttonExit = new TextButton("Exit", exitStyle);
		buttonExit.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				Locator.getAudio().playSound(SOUND.HOVER.getValue());
				Gdx.app.exit();
			}
		});
		buttonExit.pad(10.0f);

		// create title
		LabelStyle headingStyle = new LabelStyle(white96, Color.WHITE);
		heading = new Label("Puzzle Blocks", headingStyle);
		heading.setFontScale(1.0f);
		table.add(heading).expandX().padBottom(Gdx.graphics.getHeight() / 4).right();
		table.row();
		table.add(buttonClear).right();
		table.row();
		table.add(buttonPlay).right();
		table.row();
		table.add(buttonLevel).right();
		table.row();
		table.add(buttonExit).right();

		stage.addActor(table);

	}

	@Override
	public void render(float delta) {
		Gdx.gl.glClearColor(0, 0, 0, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		uiBatch.begin();
        uiBatch.draw(background, 0, 0, background.getWidth() * COUNT, background.getHeight() * COUNT, 0, COUNT, COUNT, 0);
        uiBatch.end();
		stage.act(delta);
		stage.draw();
	}

	@Override
	public void resize(int width, int height) {
		stage.setViewport(width, height, true);
		table.invalidateHierarchy();
		table.setSize(width, height);
	}

	@Override
	public void show() {
	    effects.clear();
        effects.add(new FadeOutTransitionEffect(0.5f));
        effects.add(new FadeInTransitionEffect(0.5f));
		Gdx.input.setInputProcessor(stage);
	}

	@Override
	public void hide() {
		Gdx.input.setInputProcessor(null);
	}

	@Override
	public void pause() {
	}

	@Override
	public void resume() {
	}

	@Override
	public void dispose() {
		stage.dispose();
		atlas.dispose();
		skin.dispose();
		white.dispose();
	}

}